package com.example.javanewwork.controller;


import com.example.javanewwork.entity.Produce;
import com.example.javanewwork.entity.Result;
import com.example.javanewwork.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/id")
    public int getDevice(){
        return 1;
    }


    @GetMapping("/test")
    public String getTest(){

        String p = "hello world!,this is a pirce of massage for you";


        return p;
    }

    @GetMapping("/findAlldevice")
    public Result<List<Produce>> findAlldevice(){
        List<Produce> p = deviceService.findAlldevice();
        return Result.success(p);
    }

    @GetMapping("/delete")
    public Result deleteById(@RequestParam int id){
        System.out.print(id);

        deviceService.deleteByid(id);
        return Result.success("删除成功");
    }


//    @GetMapping("/testdelete")
//    public Result testDelete(@RequestParam int id){
//        Produce p = new Produce() ;
//        p.setId(id);
//        deviceService.testdelete(id);
//        return Result.success();
//    }


    @PostMapping("/modify")
    public Result modifydevice(@RequestBody Produce produce){
        System.out.print(produce);

        int id = produce.getId();

        deviceService.modifydevice(produce,id);

        return Result.success();

    }

    @PostMapping("/add")
    public Result addnewDevice(@RequestBody Produce produce){


        System.out.print(produce);

        deviceService.addNewdevice(produce);

        return Result.success();
    }

    @GetMapping("/serach")
    public Result<List<Produce>> serachByType(@RequestParam String type){
        List<Produce> produces = deviceService.serachByType(type);
        return Result.success(produces);
    }

    @GetMapping("/graphdata")
    public Result serachstore(){

        int broken = 0;
        int useful = 0;
        int useless = 0;

        String[] typeArray = {"影像类","康复类","实验室类","家庭类","检测类","治疗类"};
//        Map<String,Integer> map = new HashMap<>();

        List<Integer[]> list = new ArrayList<>();
        for (int i = 0;i<6;i++){
            Integer[] array = new Integer[3];
            String type = typeArray[i];

            broken = deviceService.serachCount(type,0);
            useful = deviceService.serachCount(type,1);
            useless = deviceService.serachCount(type,2);
            array[0] = broken;
            array[1] = useful;
            array[2] = useless;
            list.add(array);
//            map.put("broken",broken);
        }
//        System.out.println(broken);
//        System.out.println(useful);
//        System.out.println(useless);
        return Result.success(list);
    }



}
